<?php

	function stationsQuery1() {
		$header = array("Station ID","Name","Country","Peak Temperature (20-30)"," Average Rainfall (5-10)");
		echo '<table border="1">';
		echo '<tr>';
		//Echo the table headers
		foreach($header as $s) {
			echo '<th>';
			echo	$s;
			echo '</th>';
		}	
		echo '</tr>';

		$sql = "SELECT s.stn AS stn, name, country, AES_DECRYPT(maxtemp,'@#dwave\$r') AS temp, AES_DECRYPT(prcp,'@#dwave\$r') AS prcp FROM stations AS s, dailyhistory AS d
			WHERE s.stn = d.stn
			AND d.date = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
		$result = mysql_query($sql);
		echo mysql_error();
		
		while($row = mysql_fetch_assoc($result)) {
			echo '<tr>';
			echo 	'<td>';
			echo 		'<a href="index.php?action=stationDetails1&stn='.$row['stn'].'" >';
			echo 			$row['stn'];
			echo 		'</a>';
			echo 	'</td>';
			echo 	'<td>';
			echo 		$row['name'];
			echo 	'</td>';
			echo 	'<td>';
			echo 		$row['country'];
			echo 	'</td>';
			echo 	'<td>';
			echo 		$row['temp'];
			echo 	'</td>';
			echo 	'<td>';
			echo 		$row['prcp'];
			echo 	'</td>';
			echo '</tr>';
		}
		echo '</table>';
		echo '<a href="downloads/stq1_today.php">Download current data</a><br />';
		echo '<a href="downloads/stq1_all.php">Download 30 day history (all stations)</a>';
	}
	
	function stationDetails1() {

		$sql = "SELECT s.stn AS stn, name, country, latitude ,longitude, elevation,
					AES_DECRYPT(temp,'@#dwave\$r') AS temp,
						AES_DECRYPT(dewp,'@#dwave\$r') AS dewp,
						AES_DECRYPT(stp,'@#dwave\$r') AS stp,
						AES_DECRYPT(slp,'@#dwave\$r') AS slp,
						AES_DECRYPT(visib,'@#dwave\$r') AS visib,
						AES_DECRYPT(wdsp,'@#dwave\$r') AS wdsp,
						AES_DECRYPT(prcp,'@#dwave\$r') AS prcp,
						AES_DECRYPT(sndp,'@#dwave\$r') AS sndp,
						AES_DECRYPT(frshtt,'@#dwave\$r') AS frshtt,
						AES_DECRYPT(cldc,'@#dwave\$r') AS cldc,
						AES_DECRYPT(wnddir,'@#dwave\$r') AS wnddir, 
						d.date AS date,
						AES_DECRYPT(maxtemp,'@#dwave\$r') AS maxtemp
						FROM stations AS s, dailyhistory AS d
						WHERE s.stn = d.stn
						AND s.stn = ".$_GET["stn"]."
						AND d.date = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
		$result = mysql_query($sql);
		
		echo '<h2>Station information</h2>';
		echo '<table border="1">';
		$row = mysql_fetch_assoc($result);
		if($row != null) {
			
			echo '<tr>';
			echo 	'<td>';
			echo		'Station ID';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['stn'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Name';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['name'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Country';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['country'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Latitude';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['latitude'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Longitude';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['longitude'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Elevation';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['elevation'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo	'</td>';
			echo 	'<td>';
			echo		'<a href="index.php?action=map&stn='.$row['stn'].'&lat='.$row['latitude'].'&lng='.$row['longitude'].'">View on map</a>';
			echo	'</td>';
			echo '</tr>';
			echo '</table>';
		
			echo '<h2>Station measurements '.$row['date'].'</h2>';
			echo '<table border="1">';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Temperature';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['temp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Peak. Temperature';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['maxtemp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Dew Point';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['dewp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Station Level Pressure';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['stp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Sea Level Pressure';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['slp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Visibility';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['visib'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Windspeed';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['wdsp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Precipitation';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['prcp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Snowfall';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['sndp'];
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Events';
			echo	'</td>';
			echo 	'<td>';
			echo 		readFRSHTT($row['frshtt']);
			echo	'</td>';
			echo '</tr>';
			echo '<tr>';
			echo 	'<td>';
			echo		'Avg. Cloud Coverage';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['cldc'];
			echo	'</td>';
			echo '</tr>';
			echo 	'<td>';
			echo		'Avg. Wind Direction';
			echo	'</td>';
			echo 	'<td>';
			echo		$row['wnddir'];
			echo	'</td>';
			echo '</tr>';			
		}
		echo '</table>';
		
		echo '<a href="downloads/sdq1.php?stn='.$_GET["stn"].'" >Download latest data</a>';
		
		echo '<h2>History</h2>';
		echo '<table border="1">';
		$header = array("Date","Avg. Temperature","Dew Point","Station Level Pressure","Sea Level Pressure","Visibility"
				,"Windspeed","Precipitation","Snowfall","Events","Cloud Coverage","Wind Direction","Peak Temperature");
		echo '<table border="1">';
		echo '<tr>';
		//Echo the table headers
		foreach($header as $s) {
			echo '<th>';
			echo	$s;
			echo '</th>';
		}
		echo '</tr>';
		
		$sql = "SELECT s.stn AS stn, name, country, latitude ,longitude, elevation,
					AES_DECRYPT(temp,'@#dwave\$r') AS temp,
						AES_DECRYPT(dewp,'@#dwave\$r') AS dewp,
						AES_DECRYPT(stp,'@#dwave\$r') AS stp,
						AES_DECRYPT(slp,'@#dwave\$r') AS slp,
						AES_DECRYPT(visib,'@#dwave\$r') AS visib,
						AES_DECRYPT(wdsp,'@#dwave\$r') AS wdsp,
						AES_DECRYPT(prcp,'@#dwave\$r') AS prcp,
						AES_DECRYPT(sndp,'@#dwave\$r') AS sndp,
						AES_DECRYPT(frshtt,'@#dwave\$r') AS frshtt,
						AES_DECRYPT(cldc,'@#dwave\$r') AS cldc,
						AES_DECRYPT(wnddir,'@#dwave\$r') AS wnddir, d.date AS date,
						AES_DECRYPT(maxtemp,'@#dwave\$r') AS maxtemp
					FROM stations AS s, dailyhistory AS d
					WHERE s.stn = d.stn
					AND s.stn = ".$_GET["stn"]."
					AND DATEDIFF(CURDATE(),d.date) BETWEEN 2 AND 31
					ORDER BY d.date DESC";
		
		$result = mysql_query($sql);
		
		while($row = mysql_fetch_assoc($result)) {
			echo '<tr>';
			echo 	'<td>';
			echo		$row['date'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['temp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['dewp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['stp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['slp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['visib'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['wdsp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['prcp'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['sndp'];
			echo	'</td>';
			echo 	'<td>';
			echo 		readFRSHTT($row['frshtt']);
			echo	'</td>';
			echo 	'<td>';
			echo		$row['cldc'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['wnddir'];
			echo	'</td>';
			echo 	'<td>';
			echo		$row['maxtemp'];
			echo	'</td>';
			echo '</tr>';
		}
		
		
		echo '</table>';
		
		echo '<a href="downloads/sdq1_history.php?stn='.$_GET["stn"].'" >Download history</a>';
	}
	
?>